package beanstao.util;

/**
 * 为提高 StringBuilder 的可用性而针对性的封装。事实就是一个 StringBuilder。<br>
 * 类名的命名考虑使用频繁，故简化，未按正规命名规范。
 * 
 * @author beanstao@google.com 
 */
public class Sber
{
	private final StringBuilder _Builder = new StringBuilder();

	private Sber()
	{

	}

	public static Sber ME()
	{
		Sber sber = new Sber();
		return sber;
	}

	public static Sber ME(Object obj)
	{
		Sber sber = new Sber();
		sber._Builder.append(obj);
		return sber;
	}

	public static Sber ME(Object obj, String split)
	{
		Sber sber = new Sber();
		sber._Builder.append(obj);
		return sber;
	}

	public static Sber ME(Object obj, String split, String wrapper)
	{
		Sber sber = new Sber();
		sber._Builder.append(obj);
		return sber;
	}

	public int length()
	{
		return _Builder.length();
	}

	public Sber insert(int index, Object obj)
	{
		_Builder.insert(index, obj);
		return this;
	}

	public Sber appendBlank()
	{
		_Builder.append(' ');
		return this;
	}

	public Sber append(Object obj)
	{
		_Builder.append(obj);
		return this;
	}

	public Sber append(String format, Object... args)
	{
		_Builder.append(String.format(format, args));
		return this;
	}

	/**
	 * 用前后两个包围符包围指定的值，并添加。
	 * 
	 * @param left
	 * @param value
	 * @param right
	 * @return
	 */
	public Sber surround(String left, Object value, String right)
	{
		_Builder.append(left).append(value).append(right);
		return this;
	}

	/**
	 * 用前后两个包围符包围指定的值，并添加。
	 * 
	 * @param left
	 * @param value
	 * @param right
	 * @return
	 */
	public Sber surround(String left, String right)
	{
		_Builder.insert(0, left).append(right);
		return this;
	}

	/**
	 * 用双引号包括一个对象
	 * 
	 * @param value
	 * @return
	 */
	public Sber surroundQuotation(Object value)
	{
		this.surround("\"", value, "\"");
		return this;
	}

	/**
	 * @return 用中括号包围
	 */
	public Sber surroundBracket()
	{
		this.surround("[", "]");
		return this;
	}

	/**
	 * @return 用大括号包围
	 */
	public Sber surroundBrace()
	{
		this.surround("{", "}");
		return this;
	}

	/**
	 * @return 用圆括号包围
	 */
	public Sber surroundParentheses()
	{
		this.surround("(", ")");
		return this;
	}

	/**
	 * @return 移除包围，即左右各一个字符。
	 */
	public Sber trimSurround()
	{
		_Builder.deleteCharAt(0);
		_Builder.deleteCharAt(_Builder.length() - 1);
		return this;
	}

	/**
	 * @return 移除最后一个字符
	 */
	public Sber trimEnd()
	{
		if (_Builder.length() <= 0)
			return this;
		_Builder.deleteCharAt(_Builder.length() - 1);
		return this;
	}

	public Sber trimEnd(int i)
	{
		if ((_Builder.length() <= 0) || (_Builder.length() < i))
			return this;
		_Builder.delete(_Builder.length() - i - 1, _Builder.length() - 1);
		return this;
	}

	public String end()
	{
		return this.end(1);
	}

	public String end(int i)
	{
		return _Builder.substring(_Builder.length() - 1 - i);
	}

	public Sber replace(int start, int end, String str)
	{
		_Builder.replace(start, end, str);
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return _Builder.toString();
	}

	public static void main(String[] args)
	{
	}
}
